#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "member.h"
#include "common.h"


#define MEMBER_INFO_FILE "data/member_info.dat"
#define MEMBER_ID_BASE   100001


void print_member_info(const member_info* mi);


static unsigned int get_new_mid(void)
{
	member_info mi;
	unsigned int mid;

	FILE* fp = fopen(MEMBER_INFO_FILE, "rb");

	if(fp == NULL)
	{
		return MEMBER_ID_BASE;
	}

	fseek(fp, -(long)sizeof(mi), SEEK_END);
	
	if(fread(&mi, sizeof(mi), 1, fp) == 1)
	{
		mid = mi.mid + 1;
	}
	else
	{
		mid = MEMBER_ID_BASE;
	}

	fclose(fp);

	return mid;
}


void member_reg(void)
{
	member_info mi;

	printf("请按照如下提示输入会员信息。\n\n");
	printf("卡号：");
	scanf("%s", mi.card_num);
	printf("姓名：");
	scanf("%s", mi.name);
	printf("性别（1-女,2-男）：");
	scanf("%d", (int*)&mi.s);
	printf("手机号：");
	scanf("%s", mi.phone);
	printf("会员类型（1-次卡,2-月卡,3-年卡）：");
	scanf("%d", (int*)&mi.mt);

	if(mi.mt == cika)
	{
		printf("剩余次数：");
		scanf("%d", &mi.remain_cnt);
	}

	mi.reg_time = time(NULL);

	if(mi.mt == yueka)
		mi.expire_time = mi.reg_time + 30 * 24 * 60 * 60;
	else
		mi.expire_time = mi.reg_time + 365 * 24 * 60 * 60;

	mi.mid = get_new_mid();
	mi.delete_state = 0;

	FILE* fp = fopen(MEMBER_INFO_FILE, "ab");
	
	if(fp == NULL)
	{
		perror("会员注册失败");
		return;
	}

	fwrite(&mi, sizeof(mi), 1, fp);

	fclose(fp);

	printf("\n注册会员成功！\n");
}


void member_show(void)
{
	member_info mi;
	char card_num[11];

	system("cls");  // 清屏

	printf("\n会员请刷卡！\n");
	scanf("%s",card_num);
	getchar();

	system("clear");

	FILE* fp = fopen(MEMBER_INFO_FILE, "rb");

	if(fp == NULL)
	{
		printf("\n卡片未注册，请联系工作人员处理！\n");
		return;
	}

	while(fread(&mi, sizeof(mi), 1, fp) == 1)
	{
		if(strcmp(mi.card_num, card_num) == 0)
			break;
	}

	if(feof(fp))
	{
		printf("\n卡片未注册，请联系工作人员处理！\n");
	}
	else
	{
		if(mi.delete_state == 1)
		{
			printf("\n此卡已注销，请联系工作人员处理！\n");
		}
		else
		{
			printf("\n尊贵的会员，您的个人信息如下：\n\n");
			print_member_info(&mi);
		}
	}

	fclose(fp);
	
	press_any_key();
}



void member_del(void)
{
	char num[50];
	member_info mi;

	printf("请输入要注销的会员的卡号/会员ID/手机号：");
	scanf("%s", num);
	getchar();

	FILE* fp = fopen(MEMBER_INFO_FILE, "r+");

	while(fread(&mi, sizeof(mi), 1, fp) == 1)
	{
		if(strcmp(num, mi.card_num) == 0 || strcmp(num, mi.phone) == 0 || atol(num) == mi.mid)
		{
			break;
		}
	}

	if(feof(fp))
	{
		printf("\n不存在此会员，注销失败！\n");
	}
	else
	{
		char select;

		printf("\n要注销的会员信息如下：\n");
		print_member_info(&mi);
		
		printf("\n是否确定要注销此会员(Y/N):");
		
		select = getchar();

		if(select == 'Y' || select == 'y')
		{
			mi.delete_state = 1;
			
			fseek(fp, -(long)sizeof(mi), SEEK_CUR);

			fwrite(&mi, sizeof(mi), 1, fp);

			printf("\n会员已注销！\n");
		}
	}

	fclose(fp);
}


void print_member_info(const member_info* mi)
{
	printf("卡号：%s\n", mi->card_num);
	printf("会员ID：%u\n", mi->mid);
	printf("姓名：%s\n", mi->name);
	printf("性别：%s\n", mi->s == female ? "女" : "男");
	printf("手机号：%s\n", mi->phone);

	printf("会员类型：");
	if(mi->mt == cika)
	{
		printf("次卡\n剩余次数：%d\n", mi->remain_cnt);
	}
	else if(mi->mt == yueka)
	{
		printf("月卡\n");
	}
	else if(mi->mt == nianka)
	{
		printf("年卡\n");	
	}

	struct tm* dt;

	dt = localtime(&mi->expire_time);

	printf("到期时间：%d年%d月%d日 %02d:%02d\n", dt->tm_year + 1900, dt->tm_mon + 1, dt->tm_mday, dt->tm_hour, dt->tm_min);

	dt = localtime(&mi->reg_time);

	printf("注册时间：%d年%d月%d日 %02d:%02d\n", dt->tm_year + 1900, dt->tm_mon + 1, dt->tm_mday, dt->tm_hour, dt->tm_min);
}
